{% extends 'base.html' %}
{% load static %}

{% block title %}活动列表 - Meet活动聚{% endblock %}
{% block extra_css %}
<style>
    .card-body{
        text-align: left;
    }

    /* 统一搜索和筛选表单控件高度 */
    .search-form .form-control,
    .search-form .form-select,
    .search-form .btn {
        height: 46px;
        box-sizing: border-box;
    }

    /* 确保按钮内部内容垂直居中 */
    .search-form .btn {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 5px;
    }

    /* 可选：为表单控件添加统一的外观 */
    .search-form .form-control:focus,
    .search-form .form-select:focus {
        border-color: #5a9e51;
        box-shadow: 0 0 0 0.2rem rgba(90, 158, 81, 0.25);
    }
</style>
{% endblock %}


{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col-12">
            <h3><i class="bi bi-list-ul"></i> 活动列表</h3>
        </div>
    </div>

    <!-- 搜索和筛选 -->
    <div class="row mb-4">
        <div class="col-md-12">
            <form method="get" class="row g-3 search-form">
                <div class="col-md-4">
                    <input type="text" class="form-control" name="search" placeholder="搜索活动..." value="{{ search }}">
                </div>
                <div class="col-md-3">
                    <select class="form-select" name="category">
                        <option value="">所有分类</option>
                        {% for category in categories %}
                        <option value="{{ category.id }}" {% if current_category == category.id|stringformat:"s" %}selected{% endif %}>
                            {{ category.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <select class="form-select" name="sort">
                        <option value="-created_at" {% if sort_by == '-created_at' %}selected{% endif %}>最新发布</option>
                        <option value="upcoming" {% if sort_by == 'upcoming' %}selected{% endif %}>即将开始</option>
                        <option value="popular" {% if sort_by == 'popular' %}selected{% endif %}>最受欢迎</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-search"></i> 搜索
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- 活动列表 -->
    <div class="row">
        {% for activity in activities %}
        <div class="col-md-6 col-lg-4 mb-4">
            <div class="card activity-card h-100">
                <!-- 封面图片显示 - 添加安全处理 -->
                {% if activity.cover_image %}
                    <img src="{{ activity.get_cover_image_url }}"
                         class="card-img-top activity-cover-image"
                         alt="{{ activity.title }}"
                         onerror="this.onerror=null; this.src='{% static 'imgs/default-activity-cover.jpeg' %}'">
                {% else %}
                    <div class="card-img-top activity-cover-image bg-light d-flex align-items-center justify-content-center">
                        <div class="text-center text-muted">
                            <i class="bi bi-image" style="font-size: 3rem;"></i>
                            <p class="mt-2">暂无封面图片</p>
                        </div>
                    </div>
                {% endif %}

                <span class="badge activity-badge
                    {% if activity.status == 'approved' %}
                        bg-success
                    {% else %}
                        bg-warning
                    {% endif %}">
                    {{ activity.get_status_display }}
                </span>

                <div class="card-body">
                    <h5 class="card-title">{{ activity.title }}</h5>
                    <p class="card-text text-muted">{{ activity.description|truncatewords:20 }}</p>

                    <div class="mb-2">
                        <small class="text-muted">
                            <i class="bi bi-calendar"></i> {{ activity.start_time|date:"Y-m-d H:i" }}
                        </small>
                    </div>

                    <div class="mb-2">
                        <small class="text-muted">
                            <i class="bi bi-geo-alt"></i> {{ activity.location }}
                        </small>
                    </div>

                    <div class="mb-2">
                        <small class="text-muted">
                            <i class="bi bi-people"></i> {{ activity.current_participants }}/{{ activity.max_participants }}人
                        </small>
                        {% if activity.is_full %}
                        <span class="badge bg-danger">已满</span>
                        {% endif %}
                    </div>

                    <div class="d-flex justify-content-between align-items-center">
                        <small class="text-muted">
                            <i class="bi bi-person"></i> {{ activity.creator.nickname|default:activity.creator.username }}
                        </small>
                        <a href="{% url 'activities:activity_detail' activity.id %}" class="btn btn-sm btn-primary">
                            查看详情
                        </a>
                    </div>
                </div>
            </div>
        </div>
        {% empty %}
        <div class="col-12">
            <div class="empty-state">
                <i class="bi bi-inbox"></i>
                <h4>暂无活动</h4>
                <p>快来创建第一个活动吧！</p>
                <a href="{% url 'activities:activity_create' %}" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i> 创建活动
                </a>
            </div>
        </div>
        {% endfor %}
        <!-- 分页导航 -->
        {% if page_obj.paginator.num_pages > 1 %}
            <div class="row mt-4">
                <div class="col-12">
                    <nav aria-label="活动列表分页">
                        <ul class="pagination justify-content-center">
                            <!-- 上一页 -->
                            {% if page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search %}&search={{ search }}{% endif %}{% if current_category %}&category={{ current_category }}{% endif %}{% if sort_by %}&sort={{ sort_by }}{% endif %}" aria-label="上一页">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            {% else %}
                            <li class="page-item disabled">
                                <span class="page-link" aria-hidden="true">&laquo;</span>
                            </li>
                            {% endif %}

                            <!-- 页码 -->
                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                <li class="page-item active">
                                    <span class="page-link">{{ num }}</span>
                                </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ num }}{% if search %}&search={{ search }}{% endif %}{% if current_category %}&category={{ current_category }}{% endif %}{% if sort_by %}&sort={{ sort_by }}{% endif %}">{{ num }}</a>
                                </li>
                                {% endif %}
                            {% endfor %}

                            <!-- 下一页 -->
                            {% if page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search %}&search={{ search }}{% endif %}{% if current_category %}&category={{ current_category }}{% endif %}{% if sort_by %}&sort={{ sort_by }}{% endif %}" aria-label="下一页">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                            {% else %}
                            <li class="page-item disabled">
                                <span class="page-link" aria-hidden="true">&raquo;</span>
                            </li>
                            {% endif %}
                        </ul>
                    </nav>

                    <!-- 分页信息 -->
                    <div class="text-center text-muted mt-2">
                        <small>
                            显示第 {{ page_obj.start_index }} - {{ page_obj.end_index }} 个活动，共 {{ paginator.count }} 个活动
                        </small>
                    </div>
                </div>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}